<template>
  <div class="login">
      <h3>登录页面</h3>
      <el-form :model="loginForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="账号" prop="username">
    <el-input v-model="loginForm.username" @key.enter = "summitForm"></el-input>
  </el-form-item>
  <el-form-item label="密码" prop="password">
    <el-input v-model="loginForm.password"></el-input>
  </el-form-item>
    <el-form-item label="确认密码" prop="checkpass">
    <el-input v-model="loginForm.checkpass"></el-input>
  </el-form-item>
  <el-form-item>
    <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {mapMutations,mapActions} from 'vuex'
export default {
    name:'Login',
    data(){
         var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.loginForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
        return {
            loginForm:{
                username:'',
                password:'',
                checkpass:''
            },
              rules: {
          username: [
            { required: true, message: '请输入账号', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
           password: [
            // { required: true, message: '请输入密码', trigger: 'blur' },
            // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
           checkpass: [
            // { required: true, message: '请输入确认密码', trigger: 'blur' },
            // { validator: validatePass2, trigger: 'blur' }
          ]
        }
        }
    },
      methods: {    
        ...mapMutations(['LOGIN_IN']),
        ...mapActions('loginModule',['FETCH_PERMISSION']),
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$api.getTokenInfo(this.loginForm).then(res=>{
              if(res.status===200){
                
                this.$message('登录成功');
              this.LOGIN_IN(res.data)
              this.$router.push('/')
              }else{
                console.log(res)
                this.$message('你没有权限');
                return;
              }
            })

           

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }

}
</script>

<style scoped>
.login{
    width: 600px;
    height: 400px;
    margin: 100px auto;
    background: #eee;
    

}
h3{
    padding: 30px;
    text-align: center;
}
.demo-ruleForm{
    width: 550px;
}
.el-button--primary{
    background-color: red;
    border: none;
}
.el-button:hover,.el-button:focus {
    background-color: red;
}

</style>